package com.shj.controller;

import com.alibaba.fastjson.JSONObject;
import com.shj.entity.SysRole;
import com.shj.entity.SysUser;
import com.shj.service.SysRoleService;
import com.shj.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author songhuajian
 * @Date 2020/1/13 0:05
 **/
@Controller("SysUserController")
@RequestMapping("/system/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/find")
    public String find(Model model){
        List<SysUser> sysUserList = sysUserService.findAll();
        model.addAttribute("list", sysUserList);
        return "user/user_list";
    }

    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        List<SysRole> sysRoleList = sysRoleService.findAll();
        model.addAttribute("list", sysRoleList);
        return "user/user_add";
    }

    @RequestMapping("/add")
    public String add(Model model, SysUser sysUser, @RequestParam(value = "roles") List<String> roles) {
        sysUserService.add(sysUser, roles);
        return find(model);
    }

    @RequestMapping("/openUpdate")
    public String openUpdate(Model model, String userId) {
        // 用户信息
        SysUser sysUser = sysUserService.findByUserId(userId);
        model.addAttribute("user", sysUser);
        // 角色列表
        List<SysRole> sysRoleList = sysRoleService.findAll();
        model.addAttribute("list", sysRoleList);
        return "user/user_edit";
    }

    @RequestMapping("/edit")
    public String update(Model model, SysUser sysUser, @RequestParam(value = "roles") List<String> roles){
        sysUserService.update(sysUser, roles);
        return find(model);
    }

    @RequestMapping("/delete")
    public String delete(Model model, String userId) {
        sysUserService.delete(userId);
        return find(model);
    }
}
